(function(mui, window) {
	var v3AppApi = window.v3AppApi = {};
	var baseRequest = window.baseRequest;
	var appConfig = window.appConfig;
	//var dataManager = window.dataManager;
	v3AppApi.login = function(userInfo, callback) {
		baseRequest.commonGet(appConfig.v3ServiceUrl + "/app/login", 5000, function(code, data, xhr) {
			if(code == 0) {
				var casUrl = data.cas_url;
				if(casUrl == "") {
					//已经登录
					//callback(0);
					getUserInfo("",function(code,data){
						callback(code,data);
					});
					return;
				}
				if(casUrl != "undefined") {
					//create tgt
					var postData = "username=" + userInfo.account + "&password=" + userInfo.password + "&service=" + appConfig.v3ServiceUrl + '/app/login';
					baseRequest.commonPost(casUrl, postData, 5000, function(code, data, xhr) {
						if(xhr.status == 400) {
							callback(1, dataManager.getLocale().loginTips.accountOrPwdWrong);
							return;
						}
						if(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
							var str = [];
							str = xhr.responseText.split('tickets/');
							str = str[1].split("\" ");
							var TGT = str[0];
							//获取ST （service ticket）
							var stPostData = "service=" + appConfig.v3ServiceUrl + '/app/login';
							baseRequest.commonPost(casUrl + '/' + TGT, stPostData, 5000, function(code, data, xhr) {
								if(xhr.status == 200) {
									var casST = xhr.responseText;
									//获取session
									baseRequest.commonGet(appConfig.v3ServiceUrl + '/app/login?ticket=' + casST, 5000, function(code, data, xhr) {
										if(xhr.status == 200) {
											var sessionId = JSON.parse(xhr.responseText).sessionid;
											//获取username
											getUserInfo(sessionId,function(code,data){
												callback(code,data);
												if(code !== 0){
												}
											});

										} else {
											callback(1);
										}
									});
								} else {
									callback(1);
								}
							});
						}
					});

				} else {
					callback(1, dataManager.getLocale().loginTips.getCasFailed);
				}
			} else {
				callback(code, dataManager.getLocale().loginTips.loginFailed);
			}
		});
	};

	function getUserInfo(sessionId,callback){
		//获取username
		var refresh = Math.random();
		baseRequest.commonGet(appConfig.v3ServiceUrl + '/web/cas_session?refresh=' + refresh, 5000, function(code, data, xhr) {
			if(code == 0) {
				if(data.attributes != undefined) {
					var userName = data.attributes.name;
					//plus.storage.setItem("userName", data.attributes.name);
					var loginData = {
						userName: data.attributes.name,
						sessionId:sessionId
					}
					callback(0, loginData);
				}
			} else {
				callback(1, dataManager.getLocale().loginTips.getUserInfoFailed);
			}
		});
	}

	//callback(code)  code=0表示已经登录，code=1表示没有登录
	v3AppApi.autoLogin = function(callback) {
		//以get方式调用 app/login，如果返回的casurl为“”，则表示已经登录成功
		baseRequest.commonGet(appConfig.v3ServiceUrl + "/app/login", 5000, function(code, data, xhr) {
			if(code == 0) {
				var casUrl = data.cas_url;
				if(casUrl == "") {
					//已经登录
					callback(0);
				} else {
					callback(1);
				}
			} else {
				callback(1);
			}
		});
	}

	//退出登录
	v3AppApi.logout = function(callback) {

		baseRequest.commonGet(appConfig.v3ServiceUrl + "/app/logout", 5000, function(code, data, xhr) {
//			if(code == 0) {
//				if(xhr.status == 200) {
					var casUrlLogout = data.cas_url_logout;
					baseRequest.commonGet(casUrlLogout, 5000, function(code, data, xhr) {
						console.log('000' + code);
//						if(xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
							callback(0);
//						} else {
//							callback(1);
//						}
					});
//				} else {
//					callback(1);
//				}
//			} else {
//				callback(2);
//			}
		});
	}
}(mui, window));